package com.lanou.sinasports.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.GsonNewsRecTwoDec;
import com.lanou.sinasports.utils.DoubleCache;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.LessonApplication;
import com.lanou.sinasports.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class NewsRecTwoActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TextView  tvName;
    private TextView  tvTitle;
    private TextView  picCount;
    private TextView  tvContent;
    private TextView  commentCount;
    private VolleySingleton queue;

    private ViewPagerAdapter adapter;

    private String url="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_rec_two_detail);

        viewPager= (ViewPager) findViewById(R.id.vp_news_rec_two);
        tvName= (TextView) findViewById(R.id.tv_news_rec_two_name);
        tvTitle= (TextView) findViewById(R.id.tv_news_rec_two_title);
        picCount= (TextView) findViewById(R.id.tv_news_rec_two_pic_count);
        tvContent= (TextView) findViewById(R.id.tv_news_rec_two_content);

//        commentCount= (TextView) findViewById(R.id.tv_new_rec_two_detail_comment_count);

        Intent intent=getIntent();
        String ur=intent.getStringExtra("url");
        Log.d("test", ur);
//        String ur="http://slide.sports.sina.com.cn/k/slide_2_786_94847.html?cre=sptapp&mod=f&loc=4&r=0&rfunc=44";


        String u=ur.substring(ur.lastIndexOf("slide_2_") + 8, ur.lastIndexOf(".html"));
        Log.d("test", u);
        String str[] = u.split("_");
        for(int i=0;i<str.length;i++){
            Log.i("test",str[i]);
        }

        if (str.length >= 2) {
            url = "http://platform.sina.com.cn/sports_client/slide?app_key=2586208540&_version__=3.2.0.14&__os__"
                    + "=android&format=json&sid=" + str[0] + "&album_id=" + str[1] + "&size=img&num=300&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";
        }


        queue= VolleySingleton.getInstance();

        GsonRequest<GsonNewsRecTwoDec> request=new GsonRequest<GsonNewsRecTwoDec>(url, GsonNewsRecTwoDec.class, new Response.Listener<GsonNewsRecTwoDec>() {
            @Override
            public void onResponse(GsonNewsRecTwoDec gsonNewsRecTwoDec) {
                List<String> imageList=new ArrayList<>();

                int size=gsonNewsRecTwoDec.getData().getItem().size();
                for(int i=0;i<size;i++) {
                    String title = gsonNewsRecTwoDec.getData().getItem().get(i).getTitle();
                    String name=gsonNewsRecTwoDec.getAlbum().getName();
                    String count=gsonNewsRecTwoDec.getCount();
                    String content=gsonNewsRecTwoDec.getAlbum().getIntro();
                    String image=gsonNewsRecTwoDec.getData().getItem().get(i).getImg_url();

                    Log.d("&&&&&&&&&&",content);


                    imageList.add(image);

                    tvName.setText(name);
                    tvTitle.setText(title);
                    picCount.setText("1/"+count);
                    tvContent.setText(Html.fromHtml(content));


                }

                adapter=new ViewPagerAdapter(imageList, LessonApplication.getContext());
                viewPager.setAdapter(adapter);



            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });

        queue._addRequest(request);

    }


    private class ViewPagerAdapter extends PagerAdapter{
        private List<String> data;
        private RequestQueue queue;
        private ImageLoader imageLoader;
        private Context context;
        private List<ImageView> imageViews;
        public ViewPagerAdapter(List<String> data,Context context) {
            this.data = data;
            this.context=context;
            imageViews=new ArrayList<>();
            queue= Volley.newRequestQueue(context);
            imageLoader=new ImageLoader(queue,new DoubleCache());
            for (int i = 0; i <data.size(); i++) {
                NetworkImageView imageView=new NetworkImageView(context);
                imageView.setDefaultImageResId(R.mipmap.ico_wc_title_back_p);
                imageView.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
                imageView.setImageUrl(data.get(i), imageLoader);
                imageViews.add(imageView);
            }


        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {

            view.removeView(imageViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            view.addView(imageViews.get(position));
            return imageViews.get(position);
        }
    }


}
